<?php
    class MyaccountController extends Zend_Controller_Action
    {

        public function init()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (!empty($y))
            {
                $this->user_id = $x->read()->rkn_uid;
            }
            else
            {
                $this->_redirect("index/login");
            }
            $this->user = (array) $y;
        }

        public function indexAction()
        {
            if (empty($this->user_id))
                $this->_redirect("index/login");

            $user_id = $this->user_id;
            $commonModel = new Application_Model_Common;

            $form = new Application_Form_MyaccountForm(array('userid' => $this->user_id));
            $record = $commonModel->getdata(RKN_USER, " rkn_uid = $user_id ");

            $this->view->userdata = $record;

            if ($this->getRequest()->isPost())
            {
                if ($form->isValid($this->getRequest()->getPost()))
                {
                    $arr = $form->getValues();
                    $data_array = array(
                        'username' => $arr['username'],
                        'first_name' => $arr['first_name'],
                        'last_name' => $arr['last_name'],
                        'dob' => date('Y-m-d', strtotime($arr['year'] . "-" . $arr['month'] . "-" . $arr['day'])),
                        'address' => $arr['address'],
                        'city' => $arr['city'],
                        'state' => $arr['state'],
                        'zip_code' => $arr['zip_code'],
                        'location' => $arr['country'],
                        'promotion_mail' => $arr['promotion_mail'],
                        'updates_mail' => $arr['updates_mail']
                    );
                    $commonModel->updateRecordData($data_array, ' rkn_uid = "' . $this->user_id . '" ', RKN_USER);
                    $this->_helper->FlashMessenger(
                            array('success' => '<strong>Success!</strong> You have successfully updated your account details')
                    );
                    $this->_redirect("myaccount");
                }
            }

            $this->view->form = $form;
        }

        public function orderAction()
        {
            $commonModel = new Application_Model_Common;
            $data = $commonModel->getAdapter()->select()->from(array('o'=>RKN_ORDER))
                    ->join(array('p'=>RKN_PRODUCT),new Zend_Db_Expr('find_in_set(p.rkn_prod_id,replace(o.prod_id,"|||",","))'),array('rkn_prod_id','prod_name','images','price'))
                    ->order("o.order_id desc");
            
            $data = $data->query()->fetchAll();
            $this->view->orderdata = $data;
        }

    }